package com.jiangmiao.babaisyou.level;

import com.jiangmiao.babaisyou.constant.MapConstant;
import com.jiangmiao.babaisyou.constant.PeopleLocation;
import com.jiangmiao.babaisyou.constant.TextLocation;
import com.jiangmiao.babaisyou.domain.Text;
import com.jiangmiao.babaisyou.util.GameUtil;
import com.jiangmiao.babaisyou.util.Location;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 绘制文字块
 */
public class GameText {
    private List<Text> textList; // 注意这里时java.util.list包下的List,不要导错包

    public GameText() {
        this.textList = new ArrayList<>();
//        /**
//         * 初始化GameText位置
//         */
//        this.textList.add(new Text(4*24,5*24, TextLocation.BABA));
//        this.textList.add(new Text(5*24,5*24, TextLocation.IS));
//        this.textList.add(new Text(6*24,5*24, TextLocation.YOU));
//
//        this.textList.add(new Text(12*24,5*24, TextLocation.FLAG));
//        this.textList.add(new Text(13*24,5*24, TextLocation.IS));
//        this.textList.add(new Text(14*24,5*24, TextLocation.WIN));
//
//        this.textList.add(new Text(4*24,13*24, TextLocation.WALL));
//        this.textList.add(new Text(5*24,13*24, TextLocation.IS));
//        this.textList.add(new Text(6*24,13*24, TextLocation.STOP));
//
//        this.textList.add(new Text(12*24,13*24, TextLocation.ROCK));
//        this.textList.add(new Text(13*24,13*24, TextLocation.IS));
//        this.textList.add(new Text(14*24,13*24, TextLocation.PUSH));

        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = GameUtil.loadMap("asset/map/level1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Location> locationMap = (Map<String, Location>)stringObjectMap.get(MapConstant.TEXT);
        for (String mapConstant : locationMap.keySet()) {
            Location location = locationMap.get(mapConstant);
            switch (mapConstant) {
                case MapConstant.BABA:
                    this.textList.add(new Text(location, TextLocation.BABA));break;
                case MapConstant.FLAG:
                    this.textList.add(new Text(location, TextLocation.FLAG));break;
                case MapConstant.WALL:
                    this.textList.add(new Text(location, TextLocation.WALL));break;
                case MapConstant.ROCK:
                    this.textList.add(new Text(location, TextLocation.ROCK));break;
                case MapConstant.WIN:
                    this.textList.add(new Text(location, TextLocation.WIN));break;
                case MapConstant.PUSH:
                    this.textList.add(new Text(location, TextLocation.PUSH));break;
                case MapConstant.STOP:
                    this.textList.add(new Text(location, TextLocation.STOP));break;
                case MapConstant.YOU:
                    this.textList.add(new Text(location, TextLocation.YOU));break;
                case MapConstant.IS1:
                    this.textList.add(new Text(location, TextLocation.IS));break;
                case MapConstant.IS2:
                    this.textList.add(new Text(location, TextLocation.IS));break;
                case MapConstant.IS3:
                    this.textList.add(new Text(location, TextLocation.IS));break;
                case MapConstant.IS4:
                    this.textList.add(new Text(location, TextLocation.IS));break;
            }
        }

    }

    public void draw(Graphics graphics) {
        for (int i = 0; i < textList.size(); i++) {
            Text text = textList.get(i);
            graphics.drawImage(text.getImage(),text.getX(),text.getY(),null);
        }
    }

    public List<Text> getTextList() {
        return textList;
    }

    /**
     * 判定 who is you
     * 即从上到下，从左到右的组合
     * 如果存在组合放置于list中，反之，返回null
     * @return
     */
    public List<String> whoIsYou() {
        return whoIs(TextLocation.YOU);
    }

    public List<String> whoIsStop() {
        return whoIs(TextLocation.STOP);
    }

    public List<String> whoIsPush() {
        return whoIs(TextLocation.PUSH);
    }

    public List<String> whoIsWin() {
        return whoIs(TextLocation.WIN);
    }

    /**
     * 公共代码
     * @param rule
     * @return
     */
    private List<String> whoIs(String rule) {
        // 返回结果
        List<String> res = new ArrayList<>();

        //找到textList中的you块
        Text you = null;
        for (int i = 0; i < textList.size(); i++) {
            if (textList.get(i).getRule().equalsIgnoreCase(rule)) {
                you = textList.get(i);
                break;
            }
        }
        // 横向判定
        //- you的左边第一块是is
        boolean flagL = false;
        for (int i = 0; i < textList.size(); i++) {
            Text text = textList.get(i);
            if (you.getX() - you.getWidth() == text.getX() && you.getY() == text.getY()) {
                if (text.getRule().equalsIgnoreCase(TextLocation.IS)) {
                    flagL = true; // 左边第一块是is
                    break;
                } else {
                    break; // 左边第一块不是is
                }
            }
        }
        //- you的左边第二块是可移动的角色
        if (flagL) {
            for (int i = 0; i < textList.size(); i++) {
                Text text = textList.get(i);
                if (you.getX() - 2 * you.getWidth() == text.getX() && you.getY() == text.getY()) {
                    switch (text.getRule()) {
                        case TextLocation.BABA:
                            res.add(PeopleLocation.BABA);
                            break;
                        case TextLocation.FLAG:
                            res.add(PeopleLocation.FLAG);
                            break;
                        case TextLocation.ROCK:
                            res.add(PeopleLocation.ROCK);
                            break;
                        case TextLocation.WALL:
                            res.add(PeopleLocation.WALL);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        // 纵向判定
        //- you的上边第一块是is
        boolean flagU = false;
        for (int i = 0; i < textList.size(); i++) {
            Text text = textList.get(i);
            if (you.getX() == text.getX() && you.getY() - you.getHeight() == text.getY()) {
                if (text.getRule().equalsIgnoreCase(TextLocation.IS)) {
                    flagU = true; // 上边第一块是is
                    break;
                } else {
                    break; // 上边第一块不是is
                }
            }
        }
        //- you的上边第二块是可移动的角色
        if (flagU) {
            for (int i = 0; i < textList.size(); i++) {
                Text text = textList.get(i);
                if (you.getX()  == text.getX() && you.getY() - 2 * you.getHeight() == text.getY()) {
                    switch (text.getRule()) {
                        case TextLocation.BABA:
                            res.add(PeopleLocation.BABA);
                            break;
                        case TextLocation.FLAG:
                            res.add(PeopleLocation.FLAG);
                            break;
                        case TextLocation.ROCK:
                            res.add(PeopleLocation.ROCK);
                            break;
                        case TextLocation.WALL:
                            res.add(PeopleLocation.WALL);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        if (res.size() > 0) return res;

        return null;
    }
}
